package lesson06;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 学生管理程序的界面
 */
public class StudentUI {
    private SuperArray ary = new SuperArray();
    private Scanner sc = new Scanner(System.in);
    public StudentUI(){
        ary.add(new Student("张飞","男",20));
        ary.add(new Student("关羽","男",25));
        ary.add(new Student("刘备","男",30));
        menu();
    }

    /**
     * 菜单界面
     */
    public void menu(){
        System.out.println("欢迎使用学生管理程序");
        while(true){
            System.out.println("请选择：1-查看所有学生 2-增加 3-修改 4-删除 5-退出 6-搜索");
            int input = 0;
            try{
                input = sc.nextInt();
            }catch(InputMismatchException e){
                System.out.println("输入有误");
                sc.next();
                continue;
            }

            if(input == 1){
                show();
            }else if(input == 2){
                add();
            }else if(input == 3){
                update();
            }else if(input == 4){
                remove();
            }else if(input == 5){

            }else if(input == 6){
                search();
            }
        }
    }

    /**
     * 显示所有学生
     */
    public void show(){
        for(int i = 0 ;i < ary.size();i++){
            Student student = (Student)ary.get(i);
            System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
        }
    }

    /**
     * 增加学生
     */
    public void add(){
        String name = null;
        while(true){
            System.out.println("请输入学生的姓名：（必须输入两个或以上的中文字）");
            name = sc.next();
            if(name.matches("^[\\u4e00-\\u9fa5]{2,}$")){
                break;
            }else{
                System.out.println("格式不正确，请重新输入");
            }
        }


        String gender = null;
        while(true){
            System.out.println("请输入学生的性别：（只能输入男或女）");
            gender = sc.next();
            if(gender.matches("^[男女]$")){
                break;
            }else{
                System.out.println("格式不正确，请重新输入");
            }
        }
        int age = 0;
        while(true){
            System.out.println("请输入学生的年龄：（年龄必须在18-30岁之间）");
            try{
                age = sc.nextInt();
            }catch(InputMismatchException e){
                System.out.println("必须输入数字");
                sc.next();
                continue;
            }
            if(age >= 18 && age <= 30){
                break;
            }else{
                System.out.println("格式不正确，请重新输入");
            }
        }


        ary.add(new Student(name,gender,age));
    }

    /**
     * 修改学生
     */
    public void update(){
        System.out.println("请输入你要修改的学生姓名：");
        String name = sc.next();
        for(int i = 0;i < ary.size();i++){
            Student student = (Student)ary.get(i);
            if(student.getName().equals(name)){
                System.out.println("请选择要修改的内容：1-姓名 2-性别 3-年龄");
                int input = sc.nextInt();
                if(input == 1){
                    System.out.println("请输入姓名：");
                    String newName = sc.next();
                    student.setName(newName);
                }else if(input == 2){
                    System.out.println("请输入性别：");
                    String newGender = sc.next();
                    student.setGender(newGender);
                }else if(input == 3){
                    System.out.println("请输入年龄：");
                    int newAge = sc.nextInt();
                    student.setAge(newAge);
                }
                System.out.println("修改成功");
                return;
            }
        }
        System.out.println("查无此人");
    }

    /**
     * 删除学生
     */
    public void remove(){
        System.out.println("请输入要删除的学生姓名：");
        String name = sc.next();
        for(int i = 0;i < ary.size();i++){
            Student student = (Student)ary.get(i);
            if(student.getName().equals(name)){
                ary.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("查无此人");
    }
    public void search(){
        while(true){
            System.out.println("请选择搜索的类型：1-姓名 2-性别 3-年龄");
            int input = 0;
            try{
                input = sc.nextInt();
            }catch(InputMismatchException e){
                System.out.println("输入有误");
                sc.next();
                continue;
            }
            boolean isExist = false;
            if(input == 1){
                System.out.println("请输入搜索的姓名：");
                String name = sc.next();
                for(int i = 0;i < ary.size();i++){
                    Student student = (Student)ary.get(i);
                    if(student.getName().matches("^.*"+name+".*$")){
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        isExist = true;
                    }
                }
            }else if(input == 2){
                System.out.println("请输入搜索的性别：");
                String gender = sc.next();
                for(int i = 0;i < ary.size();i++){
                    Student student = (Student)ary.get(i);
                    if(student.getGender().equals(gender)){
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        isExist = true;
                    }
                }
            }else if(input == 3){
                int age = 0;
                while(true){
                    System.out.println("请输入搜索的年龄：");
                    try{
                        age = sc.nextInt();
                        break;
                    }catch(InputMismatchException e){
                        System.out.println("输入有误");
                    }
                }
                for(int i = 0;i < ary.size();i++){
                    Student student = (Student)ary.get(i);
                    if(student.getAge() == age){
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        isExist = true;
                    }
                }
            }else{

            }
            if(!isExist){
                System.out.println("查无此人");
            }
            return;
        }


    }

    public static void main(String[] args) {
        new StudentUI();
    }
}
